<?php
/*
 *   This file is part of Verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *   Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */ 

$CREDLIST = array();

if (check("CREDENTIALSDEF")) {
	$_log->debug("Using credentials cache");
	$CREDLIST = retrieve("CREDENTIALSDEF");
} else {
	$_log->debug("Credentials cache empty");
	$defs = simplexml_load_file(ROOT_PATH."configs/credentials.xml");
	foreach ($defs->action as $act) {
		$action = new stdClass;
		$action->id=String::toString($act->attributes()->id);
		$action->name=String::toString($act->attributes()->name);
		$oplist = array();
		foreach ($act->op as $opid) {
			$actionop = new stdClass;
			$actionop->id = String::toString($opid->attributes()->id);
			$actionop->name = String::toString($opid->attributes()->name);
			$oplist[] = $actionop;
		}
		$action->op = $oplist;
		$CREDLIST[$action->id]=$action;
	}
	cache("CREDENTIALSDEF",$CREDLIST);
}	

if (!isset($param['op'])) {
	if ($param['node']=="id") {
		$res ="[";
		$gr="";
		foreach ($GROUP_LIST as $grp) {
			if ($gr!="") $gr.=", ";
			$gr .= "{id: '".$grp."', text: '".s($DEF_TRANS[String::toString($grp)])."'}";
		}
		$res .= $gr."]";
		out($res);
	} else {
		$res ="[";
		$act = "";
		foreach($CREDLIST as $defaction) {
			if ($act!="") $act.=", ";
			$act .= "{id: '".$defaction->id."', " .
					"text: '".s($defaction->name)."',leaf: true}";
		}
		$res .= $act."]";
		out($res);
	}
} else if ($op=="load") {
	$cred = new credentials;
	$list = $loader->loadListBy($cred,$cred->listByGroup($param['parent'],$param['id']));

	$res ="{results:[";
	$act = "";
	$defaction = $CREDLIST[$param['id']];
	if ($defaction!=null) {
		$_log->debug("IN ".$param['id']);
		foreach ($defaction->op as $opid) {
			if ($act!="") $act.=", ";
			$act .= "{id: '".$opid->id."', " .
					"name: '".s($opid->name)."', " .
					"active: ";
			if (inArray($list,"opid",$opid->id)) {
				$act.="'true'}";
			} else {
				$act.="'false'}";
			}
		}
		
	}	
	$res .= $act."]}";
	out($res);
} else if ($op=="save") {
	$cred = new credentials;
	$cred->groupid=$param['parent'];
	$cred->actionid=$param['node'];
	$cred->opid=$param['id'];
	$r = $loader->get($cred);
	if ($r) {
		if ($param['value']=="false") {
			$loader->delete($cred);
		}
	} else {
		if ($param['value']=="true") {
			$loader->save($cred);
		}
	}
	success();
} else if ($op=="check") {
	if ($loader->count(credentials::exists($param['id'],"*"))>0) {
		success("true");
	} else {
		success("false");
	}
} else if ($op=="copy") {
	$cr = new credentials;
	if ($param['clear']=="true") {
		$_log->debug("Clear group ".$param['target']);
		$_log->debug("Affected rows: ".$loader->execute($cr->clearGroup($param['target'])));
	}
	$_log->debug("Copy ".$param['source']." to ".$param['target']);
	$_log->debug("Affected rows: ".$loader->execute($cr->groupCopy($param['source'],$param['target'])));
	success();	
}

?>

 